//
//  DidAnnouncell.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/14.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

class DidAnnouncell: BaseTableViewCell {

    @IBOutlet weak var bgView: UIView!
    
    @IBOutlet weak var leftImageView: UIImageView!
    
    @IBOutlet weak var firstLabel: UILabel!
    
    @IBOutlet weak var secendLabel: UILabel!
    
    @IBOutlet weak var thirdLabel: UILabel!
    
    @IBOutlet weak var fourLabel: UILabel!
    
    @IBOutlet weak var fiveLabel: UILabel!
    
    
    @IBOutlet weak var inditorView: UIActivityIndicatorView!
    
    var model : DidAnnounModel?{
        
        
        didSet{
            
            if model == nil {
                return
            }
            
            
            inditorView.startAnimating()
            leftImageView.sd_setImage(with: URL(string :model!.goods_image), placeholderImage: UIImage.defaulImage(), options: []) { (image, _, _, _) in
                self.inditorView.stopAnimating()
                
            }

            
            firstLabel.text = model?.goods_name
            
//            firstLabel.text = "本期期号本期期号本期期号本期期号本期期号本期期号本期期号"

            
            secendLabel.text = "本期期号: \(model!.od_no)"
        
            
            let temp1 = "本期参与: \(model!.buy_count)人次"
            
            thirdLabel.attributedText = attributedString(str: temp1, color: UIColor.themeColor())
            
            
            let temp2 = "获奖者: \(model!.member_name)"

            
            fourLabel.attributedText = attributedString(str: temp2, color: UIColor.orange)

            
            fiveLabel.text = "揭晓时间: \(model!.publish_date)"
        
        }
    
    
    
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        
        contentView.backgroundColor = UIColor.viewBgGrey()
        
//        firstLabel.textColor = UIColor.black
//        
//        secendLabel.textColor = UIColor.dakGray()
//        thirdLabel.textColor = UIColor.dakGray()
//        fourLabel.textColor = UIColor.dakGray()
//        fiveLabel.textColor = UIColor.dakGray()
        
        bgView.addCornerAndBorder(borderColor: nil, borderWidth: 0, masksToBounds: true, cornerRadius: 5)
    
    }
    
    
    
    func attributedString(str: String, color: UIColor) -> NSAttributedString? {
        
        
        
        //创建正则表达式对象
        let patten0 = "[\u{4e00}-\u{9fa5}]{1,}"
        //创建正则表达式对象
        let patten1 = "[:]"
        
        //        let patten2 = "[¥]"    + "|" +  patten2
        
        let patten = patten0 + "|" + patten1
        
        
        //caseInsensitive  忽略大小写  最常用
        //dotMatchesLineSeparators 允许 . 匹配任意字符，包括回车换行
        let regex = try! NSRegularExpression(pattern: patten, options: NSRegularExpression.Options.caseInsensitive)
        
        //根据规则匹配指定字符串中第一个符合规则的字符串，并且返回一个NSTextCheckingResult
        let res1 = regex.matches(in: str, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range: NSRange(location: 0, length: str.characters.count))
        
        // 根据原有字符串生成属性字符串
        let strM = NSMutableAttributedString(string: str)
        
        strM.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 13), range:NSMakeRange(0, str.characters.count))
        
        strM.addAttribute(NSForegroundColorAttributeName, value: color, range:NSMakeRange(0, str.characters.count) )
        
        for temp in res1{
            
            
            strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.black, range:temp.range )
            
        }
        
        return strM
        
    }
    

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
